<?php
/**
 * Created by PhpStorm.
 * User: kun
 * Date: 26/6/19
 * Time: 18:20
 */

namespace app\api\controller\v1;


use app\api\controller\Common;
use app\common\lib\Aes;


class Upvote extends AuthBase
{
    /**
     * app点赞接口
     * @return array
     * @throws \think\exception\DbException
     */
    public function save()
    {
        $id = input('post.id', 0, 'intval');
        if (empty($id)) {
            return show(config('code.error'), 'id不存在', [], 403);
        }
        try {
            $news = model('News')->get(['id' => $id, 'status' => config('code.app_status_success')]);
        } catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [], 403);
        }

        if (is_null($news)) {
            return show(config('code.error'), '文章不存在', [], 403);
        }
        $data = [
            'user_id' => $this->user->id,
            'news_id' => $id,
        ];

        $userNews = model('UserNews')->get($data);
        if ($userNews) {
            return show(config('code.error'), '已经点赞过', [], 401);
        }

        try {
            $userNewsId = model('UserNews')->add($data);
            if ($userNewsId) {
                model('News')->where(['id' => $id])->setInc('upvote_count');
                return show(config('code.success'), 'ok', [], 202);
            } else {
                return show(config('code.error'), '内部错误，点赞失败', [], 500);
            }
        } catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [], 500);
        }

    }

    /**
     *app取消点赞接口
     */
    public function delete()
    {
        $id = input('delete.id', 0, 'intval');

        if (empty($id)) {
            return show(config('code.error'), 'id不存在', [], 403);
        }

        try {
            $news = model('News')->get(['id' => $id, 'status' => config('code.app_status_success')]);
        } catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [], 403);
        }

        if (is_null($news)) {
            return show(config('code.error'), '文章不存在', [], 403);
        }
        $data = [
            'user_id' => $this->user->id,
            'news_id' => $id,
        ];

        $userNews = model('UserNews')->get($data);
        if (empty($userNews)) {
            return show(config('code.error'), '没有被点赞过,无法取消', [], 401);
        }

        try {
            $userNewsId = model('UserNews')->where($data)->delete();
            if ($userNewsId) {
                model('News')->where(['id' => $id])->setDec('upvote_count');
                return show(config('code.success'), 'ok', [], 202);
            } else {
                return show(config('code.error'), '取消失败', [], 500);
            }
        } catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [], 500);
        }

    }

    /**
     * 查看用户是否点赞
     */
    public function read()
    {
        $id = input('param.id', 0, 'intval');

        if (empty($id)) {
            return show(config('code.error'), 'id不存在', [], 403);
        }

        try {
            $news = model('News')->get(['id' => $id, 'status' => config('code.app_status_success')]);
        } catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [], 403);
        }

        if (is_null($news)) {
            return show(config('code.error'), '文章不存在', [], 403);
        }
        $data = [
            'user_id' => $this->user->id,
            'news_id' => $id,
        ];

        $userNews = model('UserNews')->get($data);
        if ($userNews) {
            return show(config('code.success'), 'ok', ['isUpvote' => 1], 200);
        } else {
            return show(config('code.success'), 'ok', ['isUpvote' => 0], 200);
        }

    }


}

